<?php
namespace Admin\Controller;
use Think\Controller;
class CateController extends Controller{
  public function catelist(){
    $cate = D('Cate');
    $cateinfo = $cate->select();
    //让数组按照级层级排序
    $cateinfo = getTree($cateinfo);
    // dump($cateinfo);
    // die;
    $this->assign('cateinfo',$cateinfo);
    $this->display();
  }
  public function cateadd(){
    $cate = D('Cate');
    if(IS_POST){
      //如果是从post表单提交就接受数据验证后存入数据库
      $cateinfo = I('post.');
      $res=$cate->add($cateinfo);
      if($res){
        $this->success('添加成功');
      }else{
        $this->error('添加成功');
      }
    }else{
      $cateinfo = $cate->where('cate_pid = 0')->select();
      $this->assign('cateinfo',$cateinfo);
      $this->display();
    }
  }
  public function catechoose(){
    $cate = D('Cate');
    $pid = I('post.pid');
    //$pid = 2;
    //echo $pid;
    $cateainfo = $cate->where("cate_id =".$pid)->find();
    $catebinfo = $cate->where("cate_pid =".$pid)->select();
    $str =   "<li><label>上级分类</label><select class='dfinput sel' name='cate_pid'><option value='".$pid."'>--请选择--</option>";
    foreach($catebinfo as $k=>$v){
      $str .= "<option value='".$v['cate_id']."'>".$v['cate_name']."</option>";
    }
    $str .= "</select></li>";
    $info = array(
      'level' =>$cateainfo['level']+1,
      'str'   =>$str
    );
    //dump($info);
    echo json_encode($info);
  }
  public function categet(){
    $cate = D('Cate');
    $pid = I('post.pid');
    //$pid = 2;
    //echo $pid;
    $cateinfo = $cate->where("cate_pid =".$pid)->select();
    $str =   "<option value='0'>--请选择--</option>";
    foreach($cateinfo as $k=>$v){
      $str .= "<option value='".$v['cate_id']."'>".$v['cate_name']."</option>";
    }
    echo $str;
  }
}
?>
